package com.siegelion.section;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.app.Application;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.siegelion.MainActivity;
import com.siegelion.R;
import com.siegelion.databinding.ActivityFlutterBinding;

import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterEngineCache;
import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.plugin.common.BasicMessageChannel;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.StandardMessageCodec;
import jnr.ffi.annotations.In;

public class FlutterActivity extends io.flutter.embedding.android.FlutterActivity {

    ActivityFlutterBinding binding;
    private BasicMessageChannel<Object> mObjectBasicMessageChannel;
    FlutterEngine lFlutterEngine;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        flutterEngineInit("1", "sec");
        setMessageChannel(lFlutterEngine);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_flutter);
        binding.button2.setOnClickListener(v -> {
            startActivity(FlutterActivity
                    .withCachedEngine("1")
                    .build(this));
        });
        binding.button3.setOnClickListener(v -> {
            startActivity(FlutterActivity
                    .withCachedEngine("1")
                    .build(this));
        });
    }

    private void flutterEngineInit(String id, String router) {
        //创建引擎
        lFlutterEngine = new FlutterEngine(this);
        //设置一些参数
        lFlutterEngine.getNavigationChannel().setInitialRoute(router);
        //设置缓存
        lFlutterEngine.getDartExecutor().executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault());
        FlutterEngineCache.getInstance().put(id, lFlutterEngine);
    }

    private void setMessageChannel(FlutterEngine flutterEngine) {
        //信息编码
        mObjectBasicMessageChannel = new BasicMessageChannel<Object>(
                flutterEngine.getDartExecutor().getBinaryMessenger(), //Flutter 引擎
                "start_acticity",  //通道标识
                StandardMessageCodec.INSTANCE);
        //设置监听
        mObjectBasicMessageChannel.setMessageHandler(new BasicMessageChannel.MessageHandler<Object>() {
            @Override
            public void onMessage(@Nullable Object message, @NonNull BasicMessageChannel.Reply<Object> reply) {
                Log.d("message", "接收到Flutter中的消息" + message.toString());
                //mOpenFlutterTextView3.setText(message.toString());
                //if(message.equals("VueActivity")){
                Intent intent = new Intent(FlutterActivity.this, VueActivity.class);
                startActivity(intent);
                System.out.println("11111");
                //发送一个回执 只能使用一次
                reply.reply("收到消息了...");
            }
        });

    }
}


